import 'package:flutter/material.dart';

class FlappyBirdPage extends StatefulWidget {
  const FlappyBirdPage({Key? key}) : super(key: key);

  @override
  _FlappyBirdPageState createState() => _FlappyBirdPageState();
}

//这里是birdY坐标的状态

class _FlappyBirdPageState extends State<FlappyBirdPage> {
  double birdY = 0;
  bool isInterrupted = false;

  bool isRunning = false;

  startGame() {
    setState(() {
      birdY = 1;
      isRunning = true;
    });
  }

  onJumpEnd() {
    print('动画已结束调用onJumpEnd');
    setState(() {
      birdY = 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      // appBar: AppBar(
      //   title: Text('飞翔的小鸟'),
      // ),

      body: GestureDetector(
        //捕获点击事件
        onTap: () async {
          setState(() {
            birdY -= 0.5;
            print('被点击了' '现在的bird 坐标：$birdY');
          });

          Future.delayed(Duration(seconds: 2)).then((value) => () {
                setState(() {});
              });
        },
        child: isRunning
            ? Container(
                decoration: BoxDecoration(
                    border: Border.all(width: 2, color: Colors.black),
                    image: DecorationImage(
                        image: AssetImage(
                          "assets/flappybird/bg_night.png",
                        ),
                        fit: BoxFit.fill)),
                child: Column(
                  children: [
                    Expanded(
                      flex: 3,
                      child: Stack(
                        children: [
                          Positioned(
                              child: Bird(
                            birdY: birdY,
                            onEnd: onJumpEnd,
                          )),
                          Positioned(
                              child: Container(
                                  alignment: Alignment(0, -.45),
                                  child: Score(
                                    gameScore: 18,
                                  )))
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(
                        decoration: BoxDecoration(
                            //   ,
                            image: DecorationImage(
                                image: AssetImage(
                                  "assets/flappybird/land.png",
                                ),
                                fit: BoxFit.fill)),
                      ),
                    )
                  ],
                ),
              )
            : GestureDetector(
                onTap: startGame(),
                child: Container(
                  width: double.infinity,
                  height: double.infinity,
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          image: AssetImage(
                            "assets/flappybird/bg_night.png",
                          ),
                          fit: BoxFit.fill)),
                  alignment: Alignment(0, -0.2),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Image.asset(
                        "assets/flappybird/text_ready.png",
                      ),
                      Image.asset(
                        "assets/flappybird/button_resume.png",
                      ),
                    ],
                  ),
                ),
              ),
      ),
    );
  }
}

class Bird extends StatelessWidget {
  final double birdY;
  final onEnd;

  const Bird({Key? key, required this.birdY, this.onEnd}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AnimatedContainer(
      // onEnd: () {
      //   onEnd?.call();
      // },
      duration: Duration(milliseconds: 700),
      onEnd: ()
{
  onEnd?.call();
      },
      curve: Curves.easeOutSine,
      alignment: Alignment(-0.8, birdY),
      child: Container(
        width: 60,
        height: 60,
        child: Image.asset(
          "assets/flappybird/bird0_1.png",
        ),
      ),
    );
  }
}

class Score extends StatelessWidget {
  final gameScore;

  const Score({Key? key, required this.gameScore}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final gameScore2= gameScore+1;
    final a_01 = gameScore ~/ 10;
    final a_02 = gameScore % 10;
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        a_01 != 0
            ? Image.asset(
                "assets/flappybird/font_0${48 + a_01}.png",
              )
            : Container(
                width: 0,
                height: 0,
              ),
        Container(
          width: 02,
          height: 0,
        ),
        Image.asset(
          "assets/flappybird/font_0${48 + a_02}.png",
        ),
      ],
    );
  }
}
